import java.util.InputMismatchException;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) throws Exception{
        boolean flag =true;

        ZengSanGaiCha zengSanGaiCha = new ZengSanGaiCha();
        HighLevel highLevel = new HighLevel();

        System.out.println("请输入原始字符串");
        Scanner scanner1 = new Scanner(System.in);
        String orignal = scanner1.nextLine();
        System.out.println("您的原始字符串为：\n"+"============="+"\n"+orignal+"\n"+"=============");

        do {
            System.out.println("\n");
            System.out.println("字符串处理系统");
            System.out.println("请选择你想要进行的操作");
            System.out.println("0:高级操作");
            System.out.println("1:增加字符");
            System.out.println("2:删除字符");
            System.out.println("3:更改字符");
            System.out.println("4:查找字符");
            System.out.println("5:退出系统");
            System.out.println("请输入:");
            Scanner scanner = new Scanner(System.in);
            int flag_tmp = -1;
            try {
                flag_tmp = scanner.nextInt();
            }catch (Exception e){
                e.printStackTrace();
            }
            //如果输入非数字就要抛出异常，提醒输入数字
            switch (flag_tmp) {
                case 0:
                    int highchoose = highLevel.choose(orignal);
                    if (highchoose == 7) {
                        break;
                    } else {
                        flag = false;
                        break;
                    }
                case 1://增加
                    StringBuffer tmp1 = zengSanGaiCha.plus(orignal);
                    System.out.println("处理进行中");
                    System.out.println(tmp1);
                    break;
                case 2://删除
                    StringBuffer tmp2 = zengSanGaiCha.delete(orignal);
                    System.out.println("处理进行中");
                    System.out.println(tmp2);
                    break;
                case 3://更改
                    StringBuffer tmp3 = zengSanGaiCha.change(orignal);
                    System.out.println("处理进行中");
                    System.out.println(tmp3);
                    break;
                case 4://查找
                    zengSanGaiCha.check(orignal);
                    break;
                case 5:
                    flag = false;
                    break;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }while (flag);
    }
}

class ZengSanGaiCha{
    StringBuffer plus(String ori){
        System.out.println("请输入你想要增加的字符串");
        Scanner scanner = new Scanner(System.in);
        String tmp = scanner.next();
        System.out.println("你想要增加的字符串为"+ tmp);
        System.out.println("请输入你想要增加的位置");
        Scanner scanner1 = new Scanner(System.in);
        int tmp1 = scanner1.nextInt();
        //前置条件，获取字符串和位置
        StringBuffer intmp = new StringBuffer();
        intmp.append(ori);
        intmp.insert(tmp1,tmp);
        //调试中 tmp1-1,tmp
        return intmp;
    }

    StringBuffer delete(String ori){
        System.out.println("请输入你想要删除的字符串的开头位置");
        Scanner scanner = new Scanner(System.in);
        int tmp = scanner.nextInt();
        System.out.println("请输入你想要删除的字符串的结束位置");
        Scanner scanner1 = new Scanner(System.in);
        int tmp1 = scanner1.nextInt();
        //前置条件，获取字符串和位置
        StringBuffer intmp = new StringBuffer();
        intmp.append(ori);
        intmp.delete(tmp,tmp1);
        return intmp;
    }

    StringBuffer change(String ori){
        System.out.println("请输入你想要更改的字符串的开头位置");
        Scanner scanner = new Scanner(System.in);
        int tmp = scanner.nextInt();
        System.out.println("请输入你想要更改的字符串的结束位置");
        Scanner scanner1 = new Scanner(System.in);
        int tmp1 = scanner1.nextInt();
        System.out.println("请输入你想添加的字符串");
        Scanner scanner2 = new Scanner(System.in);
        String tmp2 = scanner2.next();
        System.out.println("你想要增加的字符串为"+ tmp);
        //前置条件，获取字符串和位置
        StringBuffer intmp = new StringBuffer();
        intmp.append(ori);
        intmp.replace(tmp-1,tmp1-1,tmp2);
        return intmp;
    }

    void check(String ori){
        System.out.println("请输入你想要查找的字符串");
        Scanner scanner = new Scanner(System.in);
        String  tmp = scanner.next();
        //前置条件，获取字符串
        StringBuffer intmp = new StringBuffer();
        intmp.append(ori);
        int tmp1 = intmp.indexOf(tmp)+1;
        if (tmp1 == 0){
            System.out.println("!!!查找的字符串不存在!!!");
        }else{
            System.out.println("位置为"+tmp1);
        }
    }

}

class HighLevel{
    int num = 0;
    boolean flag = true;
    public int getNum() {
        return num;
    }
    int choose(String orignal) {
        int flag_tmp = -1;
        boolean flag = true;
        do {
            System.out.println("\n");
            System.out.println("高级操作");
            System.out.println("请选择你想要进行的操作");
            System.out.println("1:取部分字符");
            System.out.println("2:判断相等");
            System.out.println("3:去空格");
            System.out.println("4:判断是否是空值");
            System.out.println("5:大小写转换");
            System.out.println("6:退出系统（返回桌面）");
            System.out.println("7:退出高级操作");
            System.out.println("请输入:");
            Scanner scanner = new Scanner(System.in);
            try {
                flag_tmp = scanner.nextInt();
            }catch (Exception e){
                e.printStackTrace();
            }
            //如果输入非数字就要抛出异常，提醒输入数字
            switch (flag_tmp) {
                case 1://取部分字符
                    StringBuffer tmp2 = select(orignal);
                    System.out.println("处理进行中");
                    System.out.println(tmp2);
                    break;
                case 2://判断相等
                    same(orignal);
                    break;
                case 3://去空格
                    System.out.println("新的字符串为\n"+"===================\n"+replacespace(orignal)+"\n===================\n");
                    break;
                case 4://判断是否是空值
                    boolean equals = orignal.isEmpty();
                    if (equals == true){
                        System.out.println("字符串为空");
                    }else{
                        System.out.println("字符串不为空");
                    }
                    break;
                case 5://大小写转换
                    String bigsmall =UpDown(orignal);
                    System.out.println("结果为"+bigsmall);
                    break;
                case 6://退出高级操作
                    flag = false;
                    break;
                case 7://退出系统（返回桌面）
                    flag = false;
                    break;
                default:
                    System.out.printf("输入有误,请输入正确选择");
            }
        } while (flag);
        return flag_tmp;
    }
    void same(String ori){
        System.out.println("请输入你想要比较的字符串");
        Scanner scanner = new Scanner(System.in);
        String tmp = scanner.next();
        //前置条件，获取对比项
        String intmp = new String();
        boolean equals = intmp.equals(tmp);
        if (equals == true){
            System.out.println("两个字符串相等");
        }else{
            System.out.println("两个字符串不相等");
        }
    }

    StringBuffer select(String ori){
        System.out.println("请输入你想要查找的字符串开头位置");
        Scanner scanner = new Scanner(System.in);
        int tmp = scanner.nextInt();
        System.out.println("请输入你想要查找的字符串结束位置");
        Scanner scanner1 = new Scanner(System.in);
        int tmp1 = scanner1.nextInt();
        //前置条件，获取位置
        StringBuffer intmp = new StringBuffer();
        intmp.append(ori);
        System.out.println("查找结果如下:\n"+intmp.substring(tmp-1,tmp1));
        return intmp;
    }

    String replacespace(String ori){
        System.out.println("清洗中");
        //前置条件
        String tmp = ori.replaceAll(" +","");
        System.out.println("清洗完毕");
        return tmp;
    }

    String UpDown(String ori){
        System.out.println("选择你的选项");
        System.out.println("1: 转为大写");
        System.out.println("2: 转为小写");
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        switch (choose){
            case 1:
                return ori.toUpperCase();
            case 2:
                return ori.toLowerCase();
            default:
                System.out.println("输入有误，返回默认值");
        }
        return ori;
    }
}